package com.dragon.mmochat.model.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

import com.dragon.mmochat.model.enumType.MmochatPayLotteryState;

public class MmochatPayFastLotteryInfo {
	private int id;
	private MmochatPayLotteryState state = MmochatPayLotteryState.活动结束;
	private int moneyNeed; // 充值条件
	private int bigValue = 0;// 奖品达到此值时添加到latestAwardInfo中
	private String startMsg = "";// 活动开始时的系统消息

	// 奖品池
	private List<MmochatFastLotteryAward> awards = new ArrayList<MmochatFastLotteryAward>();

	// 奖品名目列表
	private List<MmochatFastLotteryAwardTitle> awardTitle = new ArrayList<MmochatFastLotteryAwardTitle>();

	// 最近10条用户中奖消息
	private ConcurrentLinkedQueue<String> latestAwardInfo = new ConcurrentLinkedQueue<String>();

	// 活动期间，所有充值的用户
	private Map<Integer, MmochatFastLotteryRole> payRoles = new ConcurrentHashMap<Integer, MmochatFastLotteryRole>();

	public MmochatPayFastLotteryInfo() {
		// 保留最近10条聊天记录
		for (int i = 0; i < 10; i++) {
			latestAwardInfo.offer(new String(""));
		}
	}

	public Object[] getLatestAwardInfo() {
		return latestAwardInfo.toArray();
	}

	public void addNewAwardInfo(String msg) {
		latestAwardInfo.poll();
		latestAwardInfo.offer(msg);
	}

	// 用户充值,返回获得的抽奖次数
	public int dealRolePay(int roleId, int payMoney) {
		if (moneyNeed <= 0) {
			return 0;
		}
		int times = payMoney / moneyNeed;
		if (times > 0) {
			MmochatFastLotteryRole myInfo = payRoles.get(roleId);
			if (myInfo == null) {
				// 第一次参加
				myInfo = new MmochatFastLotteryRole();
				myInfo.setRoleId(roleId);
				payRoles.put(roleId, myInfo);
			}
			myInfo.addAwardTimes(times);
		}
		return times;
	}

	// 查询用户抽奖次数
	public int getRoleAwardTimes(int roleId) {
		MmochatFastLotteryRole myInfo = payRoles.get(roleId);
		if (myInfo == null) {
			return 0;
		}
		return myInfo.getAwardTimes();
	}

	// 初始化奖池
	public void initAwardPool() {
		Collections.shuffle(awards);
		Collections.shuffle(awards);
		Collections.shuffle(awards);

		Collections.sort(awardTitle);
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public MmochatPayLotteryState getState() {
		return state;
	}

	public void setState(MmochatPayLotteryState state) {
		this.state = state;
	}

	public int getMoneyNeed() {
		return moneyNeed;
	}

	public void setMoneyNeed(int moneyNeed) {
		this.moneyNeed = moneyNeed;
	}

	public List<MmochatFastLotteryAward> getAwards() {
		return awards;
	}

	public void setAwards(List<MmochatFastLotteryAward> awards) {
		this.awards = awards;
	}

	public int getBigValue() {
		return bigValue;
	}

	public void setBigValue(int bigValue) {
		this.bigValue = bigValue;
	}

	public void setLatestAwardInfo(ConcurrentLinkedQueue<String> latestAwardInfo) {
		this.latestAwardInfo = latestAwardInfo;
	}

	public Map<Integer, MmochatFastLotteryRole> getPayRoles() {
		return payRoles;
	}

	public void setPayRoles(Map<Integer, MmochatFastLotteryRole> payRoles) {
		this.payRoles = payRoles;
	}

	public List<MmochatFastLotteryAwardTitle> getAwardTitle() {
		return awardTitle;
	}

	public void setAwardTitle(List<MmochatFastLotteryAwardTitle> awardTitle) {
		this.awardTitle = awardTitle;
	}

	public String getStartMsg() {
		return startMsg;
	}

	public void setStartMsg(String startMsg) {
		this.startMsg = startMsg;
	}

}
